package com.zhudeshuai.zhudemolist.widget;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhu.zhuCore.utils.UIUtil;
import com.zhu.zhuCore.widget.LoadingAnimationView;
import com.zhudeshuai.zhudemolist.R;


/**
 * 加载中dialog
 *
 * @author zhudeshuai
 * @since 17/11/8
 */

public class LoadingProgressDialog extends ProgressDialog {

	private FrameLayout mIconFl;
	private TextView mMessage;

	//显示文案
	private String mMessageStr;

	//当前展示状态
	private int mCurrentStatus = Status.LOADING;

	private LoadingAnimationView mLoadingAnimationView;

	private LoadingAnimationView.LoadingAnimationViewListener mLoadingAnimationListener;

	public LoadingProgressDialog(@NonNull Context context) {
		super(context);
		setCanceledOnTouchOutside(false);
		setCancelable(false);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = getWindow();
		if (window != null) {
			window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		}
		setContentView(createContentView(getContext()));
		mIconFl = (FrameLayout) findViewById(R.id.loading_content);
		mMessage = (TextView) findViewById(R.id.toast_msg);
	}

	/**
	 * 设置显示内容
	 */
	private void setValue() {
		if (mMessage == null || mIconFl == null) return;
		//赋值
		mMessage.setText(mMessageStr);
		mIconFl.removeAllViews();
		//根据类型加载不同状态
		mLoadingAnimationView = new LoadingAnimationView(getContext());
		mLoadingAnimationView.setmLoadingAnimationViewListener(mLoadingAnimationListener);
		switch (mCurrentStatus) {
			case Status.LOADING:
				//加载中
				mLoadingAnimationView.playLoading();
				break;
			case Status.SUCCESS:
				//成功
				mLoadingAnimationView.playRight();
				break;
			case Status.FAILED:
				//失败
				mLoadingAnimationView.playWrong();
				break;
		}
		mIconFl.addView(mLoadingAnimationView);
	}


	@Override
	public void setMessage(CharSequence message) {
		mMessageStr = message == null ? "" : message.toString();
	}

	/**
	 * 创建loading View
	 *
	 * @param context 上下文
	 * @return view
	 */
	private View createContentView(Context context) {

		LinearLayout root = new LinearLayout(context);
		root.setGravity(Gravity.CENTER);
		root.setBackgroundColor(Color.TRANSPARENT);

		LinearLayout content = new LinearLayout(context);
		content.setGravity(Gravity.CENTER);
		content.setOrientation(LinearLayout.VERTICAL);
		content.setPadding(UIUtil.dip2px(15), UIUtil.dip2px(15),
				UIUtil.dip2px(15), UIUtil.dip2px(15));
		content.setBackgroundResource(R.drawable.toast_bg);
		content.setMinimumWidth(UIUtil.dip2px(100));
		content.setMinimumHeight(UIUtil.dip2px(100));
		//图标
		LinearLayout.LayoutParams paramsIcon = new LinearLayout.LayoutParams(
				UIUtil.dip2px(40), UIUtil.dip2px(40));
		paramsIcon.setMargins(0, 0, 0, UIUtil.dip2px(12));
		FrameLayout iconFl = new FrameLayout(context);
		iconFl.setId(R.id.loading_content);
		content.addView(iconFl, paramsIcon);
		//文字信息
		LinearLayout.LayoutParams paramsMsg = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

		TextView message = new TextView(context);
		message.setMaxLines(2);
		message.setEllipsize(TextUtils.TruncateAt.END);
		message.setMaxEms(10);
		message.setGravity(Gravity.CENTER);
		message.setTextColor(Color.WHITE);
		message.setId(R.id.toast_msg);
		content.addView(message, paramsMsg);

		ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		content.setLayoutParams(layoutParams);

		root.addView(content);
		return root;
	}


	/**
	 * 设置动画加载完成监听
	 *
	 * @param listener
	 */
	public void setLoadFinishListener(LoadingAnimationView.LoadingAnimationViewListener listener) {
		this.mLoadingAnimationListener = listener;
	}


	/**
	 * 显示
	 *
	 * @param status 状态
	 */
	public void show(@Status int status) {
		show();
		mCurrentStatus = status;
		setValue();

	}


	/**
	 * 显示
	 *
	 * @param status 状态
	 * @param msg    消息
	 */
	public void show(@Status int status, CharSequence msg) {
		setMessage(msg);
		show(status);
	}

	/**
	 * 状态
	 */
	@IntDef(flag = true, value = {
			Status.LOADING,
			Status.SUCCESS,
			Status.FAILED
	})
	public @interface Status {
		int LOADING = 0;
		int SUCCESS = 1;
		int FAILED = 1 << 1;
	}
}
